package wwl.lsf.hellospringboot.controller;

import lombok.Getter;

@Getter
public class HoonSynSingletonDemo {

  private static volatile HoonSynSingletonDemo instance = null;

  private static volatile boolean createFlag = false;


  private String name;

  private HoonSynSingletonDemo() {
    synchronized (HoonSynSingletonDemo.class) {
      if (createFlag) {
        throw new RuntimeException("已经实例化了");
      }
      createFlag = true;
      this.name = "ttkfz";
    }
  }

  public static HoonSynSingletonDemo getInstance() {
    if (null == instance) {
      synchronized (HoonSynSingletonDemo.class) {
        if (instance == null) {
          instance = new HoonSynSingletonDemo();
        }
      }
    }
    return instance;
  }

  private Object readResolve() {
    return getInstance();
  }

  public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
      new Thread(() -> {
        System.out.println(HoonSynSingletonDemo.getInstance().getName());
      }).start();

    }
  }

}
